package com.leyou.item.controller;


import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 品牌的查询和分页,把分页查询的数据封装到pagerResult中
     * currentPage 当前页
     * pageSize  每页显示的数量
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> page(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc,
            @RequestParam(value = "search", required = false) String search) {
        PageResult<Brand> pageResult = this.brandService.page(currentPage, pageSize, sortBy, desc, search);
        if (pageResult != null && pageResult.getItems() != null && pageResult.getItems().size() != 0) {
            return ResponseEntity.ok(pageResult);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).body(pageResult);
    }

    /**
     * 品牌的新增,
     *
     * @param brand
     * @param cids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        this.brandService.addBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 品牌的修改
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        this.brandService.updateBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 商品的新增页面,根据cid查询品牌的名称
     */

    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCategory(@PathVariable("cid") Long cid) {
        List<Brand> brands = this.brandService.queryBrandByCategory(cid);
        if (brands != null && brands.size() != 0) {
            return ResponseEntity.status(HttpStatus.OK).body(brands);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据品牌的id查询品牌的名称
     * @param brandId
     * @return
     */
    @GetMapping("brandId")
    public ResponseEntity<Brand> queryBrandByBid(@RequestParam("brandId") Long brandId) {
        Brand brand = this.brandService.queryBrandByBid(brandId);
        if (brand != null) {
            return ResponseEntity.status(HttpStatus.OK).body(brand);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
